import cv2
import numpy as np
import os
import matplotlib.pyplot as plt

# ref: https://blog.csdn.net/wumu720123/article/details/89930745

srcImgPath = os.getcwd().strip('code\ch1') + '\pic\DIP3E_Original_Images_CH02\Fig0240(a)(apollo 17_boulder_noisy).tif'

img = cv2.imread(srcImgPath, 0)
dft = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT)
# 将零频率移到图像的中心
dftShift = np.fft.fftshift(dft)
result = 20 * np.log(cv2.magnitude(dftShift[:,:,0], dftShift[:,:,1]))

plt.subplot(121)
plt.imshow(img, cmap = 'gray')
plt.title('original')
plt.axis('off')

plt.subplot(122)
plt.imshow(result, cmap = 'gray')
plt.title('result')
plt.axis('off')

plt.show()

cv2.imwrite("result.bmp", result)


